package com.runfast.waimai.web.controller.Runfastbusinesscontroller;

import com.runfast.waimai.dao.model.RunfastBusiness;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;
import com.runfast.waimai.dao.model.RunfastWithdraw;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.service.RunfastWithdrawService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/business/*")
public class runfastbusinesscontroller {
    @Resource
    RunfastBusinessService businessService;
    @Resource
    RunfastGoodsSellRecordService runfastGoodsSellRecordService;
    @Resource
    RunfastWithdrawService service;//提现表
//    @PostMapping("dirbusinessmoney") //商家销售统计 差数据 下面一个接口从定单列表中获取，要计算总和撒的
//    public @ResponseBody ModelMap dirbusinessmoney(Integer agentid,String name,String createtime1,String createtime2) throws ParseException {//代理商ID ;//商家名称 开始时间，结束时间
//        ModelMap model=new ModelMap();
//        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        Date date1=dateFormat.parse(createtime1);
//        Date date2=dateFormat.parse(createtime2);
//       if(agentid!=null&&createtime1!=null&&createtime2!=null){
//           List<RunfastBusiness> businessList=businessService.findByAgentnameAndbusinessnameAndTime(agentid,name,date1,date2);
//           if(businessList!=null){
//               model.put("list",businessList);
//           }else {model.put("msg","没有附合条件的结果"); }
//       }else {
//           model.put("msg","输入的的条件不能为空");
//       }
//        return  model;
//    }
    @PostMapping("dirbusinessmoney1") //商家销售统计 差数据 从定单列表中获取，要计算总和撒的 第五张表 business/dirbusinessmoney1
    public @ResponseBody ModelMap dirbusinessmoney1(Integer agentid,String name,String createtime1,String createtime2,Integer isdelive) throws ParseException {//代理商ID ;//商家名称 开始时间，结束时间
        ModelMap model=new ModelMap();
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date1=dateFormat.parse(createtime1);
        Date date2=dateFormat.parse(createtime2);
        if(agentid!=null&&createtime1!=null&&createtime2!=null){
           //接收的为list map 集合
            List<Map<String,String>> map =runfastGoodsSellRecordService.findByAgentnameAndbusinessnameAndTime(agentid,name,date1,date2 ,isdelive);
            if(map!=null){

                model.put("list",map);
            }else {model.put("msg","没有附合条件的结果"); }
        }else {
            model.put("msg","输入的的条件不能为空");
        }
        return  model;
    }
/*代理商销售统计
* */
@PostMapping("agentnamemoney") //第一张表
public @ResponseBody ModelMap agentnamemoney(String createtime1,String createtime2,Integer isdelive)  {
    //createatime 为查询时间，isdelive 1为自己送 0为平台送
    ModelMap model=new ModelMap();
    DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
    try {
        if(createtime1 !=null&&createtime2 !=null){
        Date date1=format.parse(createtime1);
        Date date2=format.parse(createtime2);
       List<Map<String,String>> list= runfastGoodsSellRecordService.findAgentnameMoney(date1,date2,isdelive);
       if(list!=null){
           model.put("list",list);
       }else {model.put("msg","查无结果");}}
       else {model.put("msg","时间不能为空");}
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return  model;

}
    //商家提现申请查询 第4张表
    @PostMapping("moneyhome")
    public @ResponseBody ModelMap moneyhome(String name, Integer state,Integer status,String usermobile,String createtime1,String createtime2,
                                            @RequestParam(value="page",required=false,defaultValue="0") Integer page,
                                            @RequestParam(value="pagesize",required=false,defaultValue="10") Integer pagesize) throws ParseException {
        /*String name;//商家名称  Integer state;//是否已处理1是0否 Integer status;// 状态0待审核,1通过，2不通过  String usermobile;//客户手机号
        * */
        ModelMap model=new ModelMap();

        DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Date date1=format.parse(createtime1);
        Date date2=format.parse(createtime2);
        if(name==null){
            model.put("msg","其它为空就原谅你了，但商家名不能为空");
        }else {
            List<RunfastWithdraw> runfastWithdraws=service.findMoneyhome(name,state,status,usermobile,date1,date2,page,pagesize);
            if(runfastWithdraws!=null){
                model.put("list",runfastWithdraws);
            }else {model.put("msg","你要的数据不存在");}
        }

        return model;
    }
    //商家提现记录查询 第三张表
    @PostMapping("businessmoneyhome")
    public @ResponseBody ModelMap moneyhome(String name, String createtime1, String createtime2,
                                            @RequestParam(value="page",required=false,defaultValue="0") Integer page,
                                            @RequestParam(value="pagesize",required=false,defaultValue="10") Integer pagesize) throws ParseException {
        /*String agentname;//代理商名称  Integer state;//是否已处理1是0否 Integer status;// 状态0待审核,1通过，2不通过  String usermobile;//客户手机号
         * */
        ModelMap model=new ModelMap();

        DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Date date1=format.parse(createtime1);
        Date date2=format.parse(createtime2);
        if(name==null){
            model.put("msg","其它为空就原谅你了，但商家名不能为空");
        }else {
            List<RunfastWithdraw> runfastWithdraws=service.findBusinessMoneyhome(name,date1,date2,page,pagesize);
            if(runfastWithdraws!=null){
                model.put("list",runfastWithdraws);
            }else {model.put("msg","你要的数据不存在");}
        }

        return model;
    }

}
